﻿$(function () {
    function ColBlockConf() {
        var mergsRng = [],
            dynInfo = arguments[0],
            ws = arguments[1];

        mergsRng.push({ s: { r: 1, c: 0 }, e: { r: 3, c: 0 } });//员工编号
        mergsRng.push({ s: { r: 1, c: 1 }, e: { r: 3, c: 1 } });//姓名
        mergsRng.push({ s: { r: 1, c: 2 }, e: { r: 2, c: 4 } });//加班费
        mergsRng.push({ s: { r: 1, c: 5 }, e: { r: 3, c: 5 } });//车费
        mergsRng.push({ s: { r: 1, c: 6 }, e: { r: 1, c: 14 } });//统计周期
        mergsRng.push({ s: { r: 1, c: 15 }, e: { r: 1, c: 18 } });//日期起
        mergsRng.push({ s: { r: 1, c: 20 }, e: { r: 1, c: 23 } });//日期止
        mergsRng.push({ s: { r: 1, c: 24 }, e: { r: 1, c: 5 + dynInfo.colCnt } });//（填“1”表示当天加班，并填入车费总数）
        mergsRng.push({ s: { r: 1, c: 5 + dynInfo.colCnt + 1 }, e: { r: 3, c: 5 + dynInfo.colCnt + 1 } });//备注
        mergsRng.push({ s: { r: 0, c: 0 }, e: { r: 0, c: 5 + dynInfo.colCnt + 1 } });//综合产品中心人员加班登记表
        
        ws["A2"] = { v:"员工编号",t:'s'};
        ws["B2"] = { v: "姓名", t: 's' };
        ws["C2"] = { v: "加班费", t: 's' };
        ws["F2"] = { v: "报销车费￥", t: 's' };
        ws["G2"] = { v: "统计周期:", t: 's' };
        ws["P2"] = { v: dynInfo.start, t: 's' };
        ws["T2"] = { v: "~", t: 's' };
        ws["U2"] = { v: dynInfo.end, t: 's' };
        ws["Y2"] = { v: "（填“1”表示当天加班，并填入车费总数）", t: 's' };
        ws[XLSX.utils.encode_cell({ c: 5 + dynInfo.colCnt + 1, r: 1 })] = { v: "备注", t: "s" };
        ws["C4"] = { v: "D休息", t: 's' };
        ws["D4"] = { v: "D工作", t: 's' };
        ws["E4"] = { v: "￥汇总", t: 's' };
        dynInfo.cols.forEach(function (v, i) {
            ws[XLSX.utils.encode_cell({ c: 6 + i, r: 2 })] = { v: v.num, t: "s" };
            ws[XLSX.utils.encode_cell({ c: 6 + i, r: 3 })] = { v: v.week, t: "s" };
            if (v.week == "日" || v.week == "六") {
                ws[XLSX.utils.encode_cell({ c: 6 + i, r: 3 })].s = { fill: { fgColor: { rgb: "33000000" } } };
            }
        });
        // 设置合并列信息
        ws['!merges'] = mergsRng;
        // 设置列宽信息
        ws['!cols'] = [{ wch: 5.5 }, { wch: 10 }, { wch: 7, max: 5 }, { wch: 6.875 }, { wch: 3.875, max: 6 + dynInfo.colCnt} , { wch: 16 }];
    }
    $('.form_datetime').datetimepicker({
        format:"yyyy-mm-dd",
        language: 'zh-CN',
        weekStart: 1,
        todayBtn: true,
        autoclose: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1,
        minView: 2,
        maxView: 4
    });
    $("#export").on("click", function () {
        var data = {
            _csrf: $("meta[name=x-csrf-token]").attr("content"),
            dtF: $("#dtStart").data('datetimepicker').getDate().toLocaleDateString(),
            dtT: $("#dtEnd").data('datetimepicker').getDate().toLocaleDateString()
        };
        $.post('/admin/report', data, function (data, status) {
            try {
                var data_obj = JSON.parse(data),
                    wb = new Workbook(),
                    ws = sheet_from_array_of_arrays(data_obj.rows, { row_begin: 4, defualtCellStyle: { s: { alignment: { horizontal: "center", vertical: "center" } }} }),
                    ws_name = "加班明细";
                ColBlockConf(data_obj.dnyCols,ws);
                wb.SheetNames.push(ws_name);
                wb.Sheets[ws_name] = ws;
                var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary' });
                saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), "report_test.xlsx")
            }
            catch (e) {
                showModal("系统错误","导出Excel失败:" + e.message + e.stack);
            }
        });
    });
});